class Lexer(object):
    class Node(object):
        def __init__(self):
            self.outEdge = [] # 出边的列表
            self.monitorId = None # 是否记录当前Node中通过的数据
            self.endPoint = False # 是否是终点
            self.parent = None
        def match(self, a): # 测试字符是否能在这个节点被处理（有出边）
            cnt = 0
            target = None
            for i in self.outEdge:
                if i.match(a):
                    cnt += 1
                    target = i
            if cnt > 1:
                raise
            return target
        def addOutEdge(self, a): # 添加一条出边
            if a not in self.outEdge:
                self.outEdge.append(a)
        def link(self, a, rul): # 和另一个Node连接一条边
            self.parent.linkNode(From = self, To = a, Rul = rul)
        def monitor(self, id): # 设置监视号
            self.monitorId = id
        def monitor(self): # 请求一个监视号并设置
            self.monitorId = self.parent.newMonitorId()
            return self.monitorId
        def go(self, a): # 以字符a前进一步，返回到达的节点
            if self.monitorId != None:
                self.parent.appendToMonitor(self.monitorId)
            if self.endPoint:
                return None
            ret = self.match(a)
            print("ret =", ret)
            if ret != None:
                ret = ret.go(a)
            return ret
        def setStart(self): # 设置自己为起点
            self.parent.start = self
        def setEnd(self): # 设置自己为终点
            self.endPoint = True
        def debug(self):
            print(self)
            print("outEdge", self.outEdge)
            print("endPoint", self.endPoint)
            print("monitorId", self.monitorId)
            print("id(outEdge)", id(self.outEdge))

    class Edge(object):
        def __init__(self):
            self.fromNode = None
            self.toNode = None
            self.rule = set()
            self.monitorId = None
            self.parent = None

        def link(self, From = None, To = None, Rul = set()): # 连接两个点
            self.fromNode = From
            self.toNode = To
            self.rule = Rul
            From.addOutEdge(self)
        def setRule(self, r): # 设置边规则
            self.rule = r
        def match(self, a): # 字符a能否匹配
            return a in self.rule
        def monitor(self, id): # 设置监视号
            self.monitorId = id
        def monitor(self): # 请求一个监视号并设置
            self.monitorId = self.parent.newMonitorId()
            return self.monitorId
        def go(self, a): # 以字符a前进一步，返回到达的节点
            if self.monitorId != None:
                self.parent.appendToMonitor(self.monitorId, a)
            return self.toNode
        def debug(self):
            print(self)
            print("fromNode", self.fromNode)
            print("toNode", self.toNode)
            print("rule", self.rule)
            print("monitorId", self.monitorId)
    def __init__(self):
        self.nodeList = []
        self.edgeList = []
        self.monitorList = []

    def addNode(self):
        tmp = self.Node()
        tmp.parent = self
        self.nodeList.append(tmp)
        return tmp
    def addEdge(self):
        tmp = self.Edge()
        tmp.parent = self
        self.edgeList.append(tmp)
        return tmp
    def linkNode(self, From, To, Rul):
        e = self.addEdge()
        e.link(From = From, To = To, Rul = Rul)
        return e
    def newMonitorId(self):
        print("blablablabla")
        self.monitorList.append("")
        print("len =", len(self.monitorList))
        return len(self.monitorList) - 1
    def appendToMonitor(self, id, a):
        print("len =", len(self.monitorList))
        print("id =", id)
        self.monitorList[id].append(a)

    start = None
    def setStart(self, a):
        if isinstance(a, Lexer.Node):
            self.start = a
    def setEnd(self, a):
        if isinstance(a, Lexer.Node):
            a.setEnd()
    def clearMonitorList(self):
        self.monitorList = []
    def go(self, a):
        self.clearMonitorList()
        now = self.start
        for i in a:
            print(i, now)
            if now == None:
                return False
            if now.endPoint:
                return True
            now = now.go(i)
        if now == None:
            return False
        if now.endPoint:
            return True
        return False

    def debug(self):
        print(self.nodeList)
        print(self.edgeList)
        for i in self.nodeList:
            i.debug()
        for i in self.edgeList:
            i.debug()
        print(self.monitorList)